﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;

namespace DAO
{
    public class VeChuyenBay_DAO
    {
        public static VeChuyenBay_DTO CreateVeChuyenBayDTO(VeChuyenBay vechuyenbay)
        {
            VeChuyenBay_DTO vechuyenbayDTO = new VeChuyenBay_DTO();
            Mapper.CreateMap<VeChuyenBay, VeChuyenBay_DTO>();
            vechuyenbayDTO = Mapper.Map<VeChuyenBay, VeChuyenBay_DTO>(vechuyenbay);
            return vechuyenbayDTO;
        }

        public static VeChuyenBay CreateVeChuyenBayEFF(VeChuyenBay_DTO vechuyenbayDTO)
        {
            VeChuyenBay vechuyenbay = new VeChuyenBay();
            vechuyenbay.MaVe = vechuyenbayDTO.MaVe;
            vechuyenbay.MaChuyenBay = vechuyenbayDTO.MaChuyenBay;
            vechuyenbay.MaHangVe = vechuyenbayDTO.MaHangVe;
            vechuyenbay.MaHanhKhach = vechuyenbayDTO.MaHanhKhach;
            vechuyenbay.GiaTien = vechuyenbayDTO.GiaTien;
            return vechuyenbay;
        }
        public BindingList<VeChuyenBay_DTO> GetVeChuyenBayList()
        {
            BindingList<VeChuyenBay_DTO> listVeChuyenBay = new BindingList<VeChuyenBay_DTO>();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from vechuyenbay in context.VeChuyenBays select vechuyenbay;
                foreach (var cb in query)
                {
                    VeChuyenBay_DTO vechuyenbayDTO = CreateVeChuyenBayDTO(cb);
                    listVeChuyenBay.Add(vechuyenbayDTO);
                }
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
            return listVeChuyenBay;
        }

        public static string MaVeMax()
        {
            QLCBDataContext context = new QLCBDataContext();
            string query = (from vcb in context.VeChuyenBays select vcb.MaVe).Max();
            return query;
        }

        public void InsertVeChuyenBay(VeChuyenBay_DTO vechuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                VeChuyenBay vechuyenbay = new VeChuyenBay();
                vechuyenbay.MaVe = vechuyenbayDTO.MaVe;
                vechuyenbay.MaChuyenBay = vechuyenbayDTO.MaChuyenBay;
                vechuyenbay.MaHangVe = vechuyenbayDTO.MaHangVe;
                vechuyenbay.MaHanhKhach = vechuyenbayDTO.MaHanhKhach;
                vechuyenbay.GiaTien = vechuyenbayDTO.GiaTien;
                context.VeChuyenBays.InsertOnSubmit(vechuyenbay);
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void UpdateVeChuyenBay(VeChuyenBay_DTO vechuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                VeChuyenBay vechuyenbay = new VeChuyenBay();
                vechuyenbay = context.VeChuyenBays.Single(vcb => vcb.MaVe == vechuyenbayDTO.MaVe);
                vechuyenbay.MaVe = vechuyenbayDTO.MaVe;
                vechuyenbay.MaChuyenBay = vechuyenbayDTO.MaChuyenBay;
                vechuyenbay.MaHangVe = vechuyenbay.MaHangVe;
                vechuyenbay.MaHanhKhach = vechuyenbayDTO.MaHanhKhach;
                vechuyenbay.GiaTien = vechuyenbayDTO.GiaTien;
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void DeleteVeChuyenBay(VeChuyenBay_DTO vechuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                VeChuyenBay vechuyenbay = context.VeChuyenBays.Single(cb => cb.MaVe == vechuyenbayDTO.MaVe);
                context.VeChuyenBays.DeleteOnSubmit(vechuyenbay);
                context.SubmitChanges();
            }
            catch
            { }
        }
    }
}
